ggmed_plot <- function(ames_data,
                       desc_data,
                       model_ids,
                       add_descriptives = TRUE,
                       d = "de",
                       x,
                       ylim = rep(NA_real_, 2L),
                       ytitle = "Effect",
                       xtitle = "",
                       font_size = 15L) {
  ## Data
  join_vec <- c("levels")
  names(join_vec) <- c(x)
  data <- ames_data %>%
    dplyr::filter(qoi == d) %>%
    dplyr::left_join(
      desc_data %>%
        dplyr::filter(model_id %in% model_ids) %>%
        dplyr::filter(variable %in% x) %>%
        dplyr::group_by(model_id, levels) %>%
        dplyr::summarize(counts = sum(counts, na.rm = TRUE)),
      by = join_vec
    ) %>%
    dplyr::ungroup()
  
  ## Marginal effect
  vote_plot <- data %>%
    ggplot(aes(x = !!as.name(x),
               y = est)) +
    geom_point(position = position_dodge(width = 1)) +
    geom_errorbar(aes(ymin = lower,
                      ymax = upper),
                  position = position_dodge(width = 1),
                  width = 0) +
    geom_hline(yintercept = 0, col = "red") +
    theme(panel.spacing = unit(0.75, "lines"))  +
    theme(text = element_text(size = font_size)) +
    ylim(ylim) +
    ylab(ytitle) +
    xlab(xtitle)
  
  ## Descriptives
  if (add_descriptives) {
    if (any(is.na(ylim))) {
      effective_ymin <-
        ggplot_build(vote_plot)$layout$panel_scales_y[[1]]$range$range[1]
      effective_ymax <-
        ggplot_build(vote_plot)$layout$panel_scales_y[[1]]$range$range[2]
    } else {
      effective_ymin <- ylim[1]
      effective_ymax <- ylim[2]
    }
    
    vote_plot <- vote_plot +
      geom_segment(
        aes(
          x = !!as.name(x),
          xend = !!as.name(x),
          y = effective_ymin,
          yend = ((counts / max(counts)) *
                    abs(effective_ymax - effective_ymin)) +
            effective_ymin
        ),
        size = 25,
        alpha = .1
      )
  }
  
  ## Return
  return(vote_plot)
}
